package com.face.dao;

import com.face.common.Page;
import com.face.entity.User;
import com.face.exception.DatabaseException;
import com.face.mapper.UserMapper;
import com.face.wrapper.UserWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class UserDao {
    @Autowired
    private UserMapper userMapper;

    public String saveUser(User user) {
        Integer i = userMapper.saveUser(user);
        if (i > 0)
            return user.getUserId();
        throw new DatabaseException("tb_user", "save", "user");
    }

    public User getUserById(String userId) {
        return userMapper.getUserById(userId);
    }

    public Page<User> listAll(String shopId, Integer page, Integer limit, String order, String username, String type, String creator) {
        PageHelper.startPage(page, limit);
        List<User> list = userMapper.listAll(shopId, order, username, type, creator);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Page<User> list(String shopId, Integer page, Integer limit, String start, String end, String order, String username, String type, String creator) {
        PageHelper.startPage(page, limit);
        List<User> list = userMapper.list(shopId, start, end, order, username, type, creator);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Integer count(String shopId, String start, String end) {
        return userMapper.count(shopId, start, end);
    }

    public Integer count(String shopId) {
        return userMapper.countAll(shopId);
    }

    public Integer updateUser(User user) {
        return userMapper.updateUser(user);

    }


    public List<Map<String, Object>> dailyCount(String shopId, String start, String end) {
        return userMapper.dailyCount(shopId, start, end);
    }

    public List<Map<String, Object>> monthlyCount(String shopId, String start, String end) {
        return userMapper.monthlyCount(shopId, start, end);
    }


    public UserWrapper findByUserId(String username) {
        return userMapper.findByUserId(username);
    }

    public Page<User> findUserByCreator(String openid, int page, int limit, String order) {
        PageHelper.startPage(page, limit);
        List<User> list = userMapper.findUserByCreator(openid, order);
        PageInfo<User> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Page<User> findUserForManager(String shopId, Integer page, Integer limit, String order) {
        PageHelper.startPage(page, limit);
        List<User> list = userMapper.findUserForManager(shopId, order);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public List<User> findAll(String shopId) {
        return userMapper.findAll(shopId);
    }
}
